package com.example.seey2023.spider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.seey2023.entity.ChinaTotal;
import com.example.seey2023.entity.NcovData;
import com.example.seey2023.service.ChinaTotalService;
import com.example.seey2023.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ChinaTotalScheduleTask {

    @Autowired
    private IndexService indexService;

    @Autowired
    private ChinaTotalService chinaTotalService;

    /**
     * 每小时执行一次，更新全国疫情数据情况
     *
     * @throws IOException
     */
    @Scheduled(fixedDelay = 1000*60*60*24) //1h*24
    //@Scheduled(cron = "0 0 12 * * ?")
    public void updateChinaTotalToDB() throws IOException, ParseException {
        System.out.println("远端服务已停止提供数据，定时任务不再抓取数据。");
        /*
        HttpUtils httpUtils = new HttpUtils();
        String string = httpUtils.getData();
//        System.out.println("初始数据：" + string);

        // 1.所有数据的alibaba格式
        JSONObject jsonObject = JSONObject.parseObject(string);
        Object data = jsonObject.get("data");
//        System.out.println("data数据:" + data);
        // 2.data
        JSONObject jsonObjectData = JSONObject.parseObject(data.toString());
        Object chinaTotal = jsonObjectData.get("chinaTotal");
        Object lastUpdateTime = jsonObjectData.get("overseaLastUpdateTime");
//        System.out.println("chinaTotal数据：" + chinaTotal);
        // 3. 1号total 全中国整体疫情数据
        JSONObject jsonObjectTotal = JSONObject.parseObject(chinaTotal.toString());
        Object total = jsonObjectTotal.get("total");
//        System.out.println("total数据:" + total);
        // 4.全国数据total
        JSONObject totalData = JSONObject.parseObject(total.toString());
        Object confirm = totalData.get("confirm");
        Object input = totalData.get("input");
        Object severe = totalData.get("severe");
        Object heal = totalData.get("heal");
        Object dead = totalData.get("dead");
        Object suspect = totalData.get("suspect");
        // System.out.println("confirm数据：" + confirm);

        // 5.为程序实体赋值
        ChinaTotal dataEntity = new ChinaTotal();
        dataEntity.setConfirm((Integer) confirm);
        dataEntity.setInput((Integer) input);
        dataEntity.setSevere((Integer) severe);
        dataEntity.setHeal((Integer) heal);
        dataEntity.setDead((Integer) dead);
        dataEntity.setSuspect((Integer) suspect);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dataEntity.setUpdateTime(format.parse(String.valueOf(lastUpdateTime)));

        // 6.插入数据库【更新】
        chinaTotalService.save(dataEntity);



         // 取各省份数据
        // 获取areaTree【data{areaTree{0,1,2{today,total},3...
        JSONArray areaTree = jsonObjectData.getJSONArray("areaTree");
        Object[] objects = areaTree.toArray();
        // 遍历所有国家
//        for (int i = 0; i < objects.length; i++) {
//            JSONObject jsonObject1= JSONObject.parseObject(objects[i].toString());
//            Object name = jsonObject1.get("name");
//            System.out.println(name);
//        }
        // 获取中国疫情数据
        JSONObject jsonObject1 = JSONObject.parseObject(objects[2].toString());
        JSONArray children = jsonObject1.getJSONArray("children");
        Object[] objects1 = children.toArray();//各省份

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<NcovData> list = new ArrayList<>();
        for (int i = 0; i < objects1.length; i++) {
            NcovData ncovData = new NcovData();
            JSONObject jsonObject2 = JSONObject.parseObject(objects1[i].toString());
            Object name = jsonObject2.get("name");//省份名
            Object lastUpdateTimeProvince = jsonObject2.get("lastUpdateTime");//省份更新数据时间
            Object total1 = jsonObject2.get("total");
            JSONObject jsonObject3=JSONObject.parseObject(total1.toString());//total
            Object confirm1 = jsonObject3.get("confirm");//累计确诊

            // 获取 累计死亡、累计治愈
            Object heal1 = jsonObject3.get("heal");
            Object dead1 = jsonObject3.get("dead");
            // int todayConfirm = (int) confirm1 - (int)heal1 - (int) dead1;
            int todayConfirm01 = (int) confirm1 - (int)heal1 - (int) dead1;
            int todayConfirm;
            if (todayConfirm01 > 0) {
                 todayConfirm = todayConfirm01;
            }else {
                todayConfirm = 0;
            }

            //System.out.println(name+"-累计确诊："+confirm1);
            ncovData.setName(name.toString());
            ncovData.setValue(todayConfirm);

            if(lastUpdateTimeProvince == null){
                ncovData.setUpdateTime(new Date());
            }else {
                ncovData.setUpdateTime(format1.parse(String.valueOf(lastUpdateTimeProvince)));
            }
            list.add(ncovData);

        }

        // 各个省份
        indexService.saveBatch(list);

        // 7.删除缓存【非常重要 保证redis和MySQL数据一致性的简单套路】
        Jedis jedis = new Jedis("127.0.0.1");
        if (jedis!=null){
            jedis.flushDB();
        }
        */

    }
}
